package api

import (
	"encoding/json"
	"fmt"

	"github.com/hyperledger/fabric/core/chaincode/shim"
	pb "github.com/hyperledger/fabric/protos/peer"

	"chaincode/model"
	"chaincode/pkg/utils"
)

func CreateHouseTest(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	// 验证参数
	if len(args) != 4 {
		return shim.Error("参数个数不满足")
	}
	owner := args[0]
	idCard := args[1]
	houseId := args[2]
	status := args[3]
	if owner == "" || idCard == "" || houseId == "" || status == "" {
		return shim.Error("参数存在空值")
	}
	//创建信息对象
	houseTest := &model.HouseTestMessage{
		Owner:   owner,
		IdCard:  idCard,
		HouseId: houseId,
		Status:  status,
	}
	// 写入账本
	if err := utils.WriteLedger(houseTest, stub, model.HouseTest, []string{houseTest.Owner, houseTest.HouseId}); err != nil {
		return shim.Error(fmt.Sprintf("%s", err))
	}

	//将成功创建的信息返回
	sellingByte, err := json.Marshal(houseTest)
	if err != nil {
		return shim.Error(fmt.Sprintf("序列化成功创建的信息出错: %s", err))
	}
	// 成功返回
	return shim.Success(sellingByte)
}
